package com.andyage.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import com.andyage.admin.bean.AditemBean;
import com.andyage.admin.bean.AdorderBean;
import com.andyage.admin.bean.LotteryBean;
import com.andyage.admin.dao.AdItemMapper;
import com.andyage.admin.dao.AdOrderMapper;
import com.andyage.admin.dao.LotteryMapper;
import com.andyage.admin.service.IAdorderService;
import com.andyage.util.DateUtil;
import com.andyage.util.ExcelIndexName;
import com.andyage.util.GetUUID;
import com.andyage.util.JsonContent;
import com.andyage.util.MD5;
import com.andyage.util.TypeTransfor;

import net.sf.json.JSONObject;

@Service
public class AdorderServiceImpl implements IAdorderService {

	@Resource
	private AdOrderMapper adorderMapper;
	@Resource
	private LotteryMapper lotteryMapper;
	@Resource
	private AdItemMapper aditemMapper;
	@Override
	public String queryAdOrderInfo(AdorderBean order) throws Exception {
		List<AdorderBean> list = adorderMapper.queryAdOrderInfo(order);
		int total = adorderMapper.queryAdOrderInfoCount(order);
		String json = JsonContent.getJsonArray(list, total);
		return json;
	}

	@Override
	public String deleteAdOrderInfo(String orderId) throws Exception {
		int flag = adorderMapper.deleteAdOrderInfo(orderId);
		JSONObject json = new JSONObject();
		if (flag != 0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "删除成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "删除失败");
		}
		return json.toString();
	}

	@Override
	public String updateAdorderInfo(AdorderBean order) throws Exception {
		int flag = adorderMapper.updateAdorderInfo(order);
		JSONObject json = new JSONObject();
		if (flag != 0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "修改成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "修改失败");
		}
		return json.toString();
	}

	@Override
	public HSSFWorkbook exportAdorderInfoByCondition(AdorderBean order) throws Exception {
		List<AdorderBean> list = adorderMapper.queryAdOrderInfo(order);
		 // 第一步，创建一个webbook，对应一个Excel文件  
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("订单表1");
		sheet.setDefaultColumnWidth(20);
		// 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = sheet.createRow(0);
		// 第四步，创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		style.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);
		HSSFCell cell = null;
		Map<Integer, String> map = ExcelIndexName.orderMap();
		for (int a = 0; a < map.size(); a++) {
			cell = row.createCell(a);
			cell.setCellValue(map.get(a));
			cell.setCellStyle(style);
		}
		if(!list.isEmpty()){
			for (int i = 0; i < list.size(); i++) {
				AdorderBean aorder = list.get(i);
				if (aorder != null) {
					row = sheet.createRow(i + 1);
					if(aorder.getProjectTitle()!=null){
						
						row.createCell(0).setCellValue(aorder.getProjectTitle());
					}
					if(aorder.getItemTitle()!=null){
						row.createCell(1).setCellValue(aorder.getItemTitle());
					}
					if(aorder.getOrderPrice()!=null){
						row.createCell(2).setCellValue(aorder.getOrderPrice());
					}
					if(aorder.getOrderPayStatus()!=null){
						row.createCell(3).setCellValue(TypeTransfor.payStatus(aorder.getOrderPayStatus()));
					}
					if(aorder.getOrderPayMethod()!=null){
						row.createCell(4).setCellValue(aorder.getOrderPayMethod());
					}
					if(aorder.getOrderPayNumber()!=null){
						row.createCell(5).setCellValue(aorder.getOrderPayNumber());
					}
					if(aorder.getOrderCreateTime()!=null){
						row.createCell(6).setCellValue(DateUtil.getDateFull(aorder.getOrderCreateTime()));
					}
					if(aorder.getUserName()!=null){
						row.createCell(7).setCellValue(aorder.getUserName());
					}
					if(aorder.getUserTelephone()!=null){
						row.createCell(8).setCellValue(aorder.getUserTelephone());
					}
					if(aorder.getOrderReceiverName()!=null){
						row.createCell(9).setCellValue(aorder.getOrderReceiverName());
					}
					if(aorder.getOrderReceiverTelephone()!=null){
						row.createCell(10).setCellValue(aorder.getOrderReceiverTelephone());
					}
					if(aorder.getOrderReceiverAddress()!=null){
						row.createCell(11).setCellValue(aorder.getOrderReceiverAddress());
					}
					if(aorder.getOrderDescription()!=null){
						row.createCell(12).setCellValue(aorder.getOrderDescription());
					}

				}
			}
		}
		
		return wb;
	}

	@Override
	public String queryChoujiangOrder(AdorderBean order) throws Exception {
		List<AdorderBean> list = adorderMapper.queryChoujiangOrder(order);
		int total = adorderMapper.queryChoujiangOrderCount(order);
		String json = JsonContent.getJsonArray(list, total);
		return json;
	}

	@Override
	public String addWinningOrder(AdorderBean order, int count) throws Exception {
		List<AdorderBean> list = adorderMapper.queryChoujiangOrder(order);
		Random random = new Random();
		List<LotteryBean> lotteryList=new ArrayList<LotteryBean>();
		AditemBean aditem=new AditemBean();
		if (!list.isEmpty()) {
			aditem.setItemId(list.get(0).getItemId());
			for (int a = 0; a < count; a++) {
				int randomNum = random.nextInt(list.size());
				String orderId = list.get(randomNum).getOrderId();// 随机获得list中随机条数的id
				LotteryBean lottery = new LotteryBean();
				lottery.setLotteryId(GetUUID.get());
				lottery.setOrderId(orderId);
				lottery.setLotteryCreateTime(new Date());
				lotteryList.add(lottery);
				list.remove(randomNum);//抽中后删除抽中的数据继续抽，直到抽完
			}
			aditem.setItemAwardStatus(3);
			aditemMapper.updateAditem(aditem);//改变抽奖状态
			int flag = lotteryMapper.insertLotteryInfo(lotteryList);
			JSONObject json = new JSONObject();
			if (flag != 0) {
				json.accumulate("result", 1);
				json.accumulate("tips", "成功抽奖"+flag+"次");
			} else {
				json.accumulate("result", 0);
				json.accumulate("tips", "抽奖失败");
			}
			return json.toString();
	}
		return null;
	}
}
